package com.vic.kjlink.order.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vic.kjlink.order.lookup.OrdersLookup;
import com.vic.kjlink.order.model.Orders;
import com.vic.kjlink.order.model.Start;
import com.vic.kjlink.order.service.OrdersService;
import com.vic.kjlink.order.service.StartService;

import pers.vic.boot.base.model.BaseSelectModel;
import pers.vic.boot.base.model.PageInfo;
import pers.vic.boot.base.model.Remind;
import pers.vic.boot.util.poi.export.ExportExcel;

/**
 * @author Vic.xu
 * @date 2021/08/18
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseOrderController {

    @Resource
    private OrdersService ordersService;

    @Resource
    private StartService startService;

    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public String list(OrdersLookup lookup, Model model) {
        PageInfo pager = ordersService.page(lookup);
        pager.setLookup(lookup);
        model.addAttribute("pager", pager);

        return "order-list";
    }

    /**
     * 点餐页面
     *
     * @return
     */
    @GetMapping(value = {"", "/"})
    public String order(Model model) {
        Start lastOpen = startService.getLastOpen();
        if (lastOpen == null || lastOpen.getId() == null) {
            model.addAttribute(Remind.danger().setMessage("非点餐时间，若需点餐，请先联系管理员"));
        } else {
            Orders order = ordersService.getTodayUserOrder(lastOpen.getId(), currentUserCode());
            if (order != null) {
                model.addAttribute("id", order.getId());
            }
            List<BaseSelectModel> orders = ordersService.todayOrders(lastOpen, order);
            model.addAttribute("startId", lastOpen.getId());
            model.addAttribute("orders", orders);
        }

        return "order-form";
    }

    /**
     * 某一次开启的点餐详情
     */
    @GetMapping(value = "/detail/{startId}")
    public String details(@PathVariable int startId, Model model) {
        OrdersLookup lookup = new OrdersLookup();
        lookup.setStartId(startId);
        List<Orders> list = ordersService.list(lookup);
        model.addAttribute("datas", list);
        return "order-detail";
    }

    @PostMapping(value = "/save")
    public String save(Orders order, RedirectAttributes attrs) {
        ordersService.save(order);
        attrs.addFlashAttribute(Remind.success());
        return "redirect:/order/list";
    }

    /**
     * 取消订餐
     * 
     * @param id
     * @param attrs
     * @return
     */
    @GetMapping(value = "/cancle/{id}")
    public String save(@PathVariable int id, RedirectAttributes attrs) {
        ordersService.delete(id);
        attrs.addFlashAttribute(Remind.success().setMessage("成功取消订餐"));
        return "redirect:/order/list";
    }

    /**
     * 导出最后
     */
    @GetMapping(value = "exportLatest")
    public void exportLatest() throws IOException {
        OrdersLookup lookup = new OrdersLookup();
        lookup.setLatest(true);
        export(lookup);
    }

    /**
     * 根据查询导出
     */
    @RequestMapping(value = "exportAll")
    public void exportAll(OrdersLookup lookup) throws IOException {
        export(lookup);
    }

    private void export(OrdersLookup lookup) throws IOException {
        List<Orders> list = ordersService.list(lookup);
        new ExportExcel("订餐列表", Orders.class).setDataList(list)
            .write(response, System.currentTimeMillis() + "订餐列表.xlsx").dispose();
    }

}
